package com.mkvld.appietijdenv2.data;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.mkvld.appietijdenv2.data.Shift.Allowance;

public class PeriodShifts implements Iterable<WeekShifts>, Serializable {
	private static final long serialVersionUID = 1772070113782941802L;
	private WeekShifts[] shifts;
	private double workTime, sickHours;
	private Allowance allowance;
	
	public PeriodShifts() {
		shifts = new WeekShifts[4];
		for (int i = 0; i < 4; i++)
			shifts[i] = new WeekShifts();
	}
	
	public PeriodShifts(int contract, List<Shift> shifts) {
		this.shifts = new WeekShifts[4];
		for (int i = 0; i < 4; i++) {
			this.shifts[i] = new WeekShifts();
		}
		for (Shift shift : shifts) {
			this.shifts[shift.getWeek() % 4].add(shift);
		}
		calulateTimes(contract);
	}
	
	public void calulateTimes(int contract) {
		workTime = 0;
		sickHours = 0;
		allowance = new Allowance();
		
		for (WeekShifts weekShifts : shifts) {
			weekShifts.calculateTimes(contract);
			
			workTime += weekShifts.getWorkTime();
			sickHours += weekShifts.getSickHours();
			allowance.add(weekShifts.getAllowance());
		}
	}
	
	public double getWorkTime() {
		return workTime;
	}
	
	public double getSickHours() {
		return sickHours;
	}
	
	public Allowance getAllowance() {
		return allowance;
	}
	
	public WeekShifts getWeekShifts(int week) {
		return shifts[week % 4];
	}
	
	public double getTotal() {
		return workTime + sickHours + allowance.getTotal();
	}

	@Override
	public Iterator<WeekShifts> iterator() {
		return Arrays.asList(shifts).iterator();
	}

	public void add(Shift shift) {
		shifts[shift.getWeek() % 4].add(shift);
	}
}
